//
//  YJCVisitorController.m
//  xiangzhe
//
//  Created by lyt on 16/11/16.
//  Copyright © 2016年 com.dazhebao. All rights reserved.
//

#import "YJCVisitorController.h"
#import "YTTimeButton.h"
#import "YJCDzbLogin.h"
#import "YJCIsMobile.h"
#import "YJCRegistLogin.h"
#import "YJCUserLogin.h"
#import "MJExtension.h"
#import "YJCTabBarController.h"
#import <UMSocialCore/UMSocialCore.h>
#import "YJCRelateLogin.h"

@interface YJCVisitorController ()<UINavigationControllerDelegate>
// 获取手机验证码按钮
@property(nonatomic, strong) YTTimeButton *sendBtn;
// 手机号
@property(nonatomic, strong) UITextField *phoneNum;
//验证码
@property(nonatomic, strong) UITextField *safeCode;
@end

@implementation YJCVisitorController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_denglu"]];
    self.navigationController.navigationBar.hidden = YES;
    [self setUpUI];
    
}

- (void)dealloc{
    NSLog(@"挂了");
}

- (void)qqImgClick{
    NSLog(@"qq");
    [self getUserInfoForPlatform:UMSocialPlatformType_QQ];
}
- (void)wxImgClick{
    NSLog(@"微信");
    [self getUserInfoForPlatform:UMSocialPlatformType_WechatSession];
}
- (void)tbImgClick{
    NSLog(@"淘宝");
//    [self getUserInfoForPlatform:UMSocialPlatformType_Predefine_Begin];
}
- (void)wbImgClick{
    [self getUserInfoForPlatform:UMSocialPlatformType_Sina];

    
}
// 第三方登录
- (void)getUserInfoForPlatform:(UMSocialPlatformType)platformType
{
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platformType currentViewController:self completion:^(id result, NSError *error) {
        UMSocialUserInfoResponse *userinfo =result;
       
        NSLog(@"-------%@--------",userinfo.gender);
        // 返回数据
        YJCRelateLogin *relateVC = [[YJCRelateLogin alloc] init];
        relateVC.userName = userinfo.name;
        relateVC.userIcon = userinfo.iconurl;
        [self.navigationController pushViewController:relateVC animated:YES];
    }];
}


// 登录按钮
- (void)loginBtnClick{
    // 退出键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    if (self.phoneNum.text.length == 11 && self.safeCode.text.length > 0) {
        [SVProgressHUD showWithStatus:@"正在登录"];
        
        [[YJCNetWork sharedNetWork] requestWithMethod:POST serviceName:@"User.Quick_login" parameters:@{@"phone":self.phoneNum.text,@"code":self.safeCode.text} finshed:^(id result, NSError *error) {
            NSLog(@"%@---%@",result,error);
            NSDictionary *resultInfo = result[@"data"][@"resultInfo"];
            NSString *resultCode = result[@"data"][@"resultCode"];
            YJCUserLogin *userLogin = [YJCUserLogin mj_objectWithKeyValues:resultInfo];
            if (resultCode.intValue == 0) {
                [SVProgressHUD dismiss];
                // 存储数据
                [NSKeyedArchiver archiveRootObject:userLogin toFile:kFilePath];
                //                [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
                NSLog(@"跳转到主页");
                
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
                
            }else{
                NSString *mg = result[@"data"][@"resultInfo"];
                NSLog(@"网络错误 %@",mg);
                [SVProgressHUD showImage:nil status:mg];
            }
        }];
    }else{
        [SVProgressHUD showImage:nil status:@"手机号或者验证码错误"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, KScreenH / 4)];
    }
    
}
//手机验证码发送
- (void)sendMessage{
    // 退出键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    //获取到手机号 验证有效性 发送网络请求
    
    if ([YJCIsMobile stringIsMobile:self.phoneNum.text]) {
        self.sendBtn.userInteractionEnabled = YES;
        [self.sendBtn beginTime:60 title:@"重新发送" sendingColor:[UIColor lightGrayColor] finishColor:[UIColor lightGrayColor]];
        
        [[YJCNetWork sharedNetWork] requestWithMethod:POST serviceName:@"User.SendMobileCode" parameters:@{@"phone":self.phoneNum.text} finshed:^(id result, NSError *error) {
            NSLog(@"%@",result);
            
        }];
        
    }else{
        [SVProgressHUD showImage:nil status:@"请输入正确的手机号"];
        [SVProgressHUD setOffsetFromCenter:UIOffsetMake(0, KScreenH / 4)];
        
    }
    
}
- (void)dzbBtnClick{
    NSLog(@"打折宝");
    YJCDzbLogin *dzbVC = [[YJCDzbLogin alloc] init];
    [self.navigationController pushViewController:dzbVC animated:YES];
}

- (void)registBtnClick{
    NSLog(@"注册");
    [self.navigationController pushViewController:[[YJCRegistLogin alloc] init] animated:YES];
}
- (void)backVC{
    [SVProgressHUD dismiss];
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - UI 及页面布局
- (void)setUpUI{
    // 登录标题
    UILabel *titleLab = [[UILabel alloc] init];
    titleLab.text = @"登录";
    titleLab.font = [UIFont systemFontOfSize:17];
    titleLab.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLab];
    // qq 标识
    UIImageView *qqImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_qqicon"]];
    [qqImg sizeToFit];
    qqImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *qqGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(qqImgClick)];
    [qqImg addGestureRecognizer:qqGesture];
    [self.view addSubview:qqImg];
    UILabel *qqLab = [[UILabel alloc] init];
    qqLab.text = @"qq";
    qqLab.textAlignment = NSTextAlignmentCenter;
    qqLab.font = [UIFont systemFontOfSize:12];
    qqLab.textColor = [UIColor yt_colorWithHexString:@"#949494"];
    [self.view addSubview:qqLab];
    
    //微信标识
    UIImageView *wxImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_weixinicon"]];
    [wxImg sizeToFit];
    [self.view addSubview:wxImg];
    wxImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *wxGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wxImgClick)];
    [wxImg addGestureRecognizer:wxGesture];
    //名称
    UILabel *wxLab = [[UILabel alloc] init];
    wxLab.text = @"微信";
    wxLab.textAlignment = NSTextAlignmentCenter;
    wxLab.font = [UIFont systemFontOfSize:12];
    wxLab.textColor = [UIColor yt_colorWithHexString:@"#949494"];
    [self.view addSubview:wxLab];
    //淘宝标识
    UIImageView *tbImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_taobaoicon"]];
    [tbImg sizeToFit];
    [self.view addSubview:tbImg];
    tbImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *tbGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tbImgClick)];
    [tbImg addGestureRecognizer:tbGesture];
    //名称
    UILabel *tbLab = [[UILabel alloc] init];
    tbLab.text = @"淘宝";
    tbLab.textAlignment = NSTextAlignmentCenter;
    tbLab.font = [UIFont systemFontOfSize:12];
    tbLab.textColor = [UIColor yt_colorWithHexString:@"#949494"];
    [self.view addSubview:tbLab];
    
    //微博标识
    UIImageView *wbImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_weiboicon"]];
    [wbImg sizeToFit];
    [self.view addSubview:wbImg];
    wbImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *wbGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(wbImgClick)];
    [wbImg addGestureRecognizer:wbGesture];
    //名称
    UILabel *wbLab = [[UILabel alloc] init];
    wbLab.text = @"微博";
    wbLab.textAlignment = NSTextAlignmentCenter;
    wbLab.font = [UIFont systemFontOfSize:12];
    wbLab.textColor = [UIColor yt_colorWithHexString:@"#949494"];
    [self.view addSubview:wbLab];
    // 3条分割线
    UIView *topView = [[UIView alloc] init];
    UIView *leftView = [[UIView alloc] init];
    UIView*rightView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    leftView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    rightView.backgroundColor = [UIColor yt_colorWithHexString:@"#efefef"];
    [self.view addSubview:topView];
    [self.view addSubview:leftView];
    [self.view addSubview:rightView];
    //提示登录方式
    UILabel *signLab = [[UILabel alloc] init];
    signLab.text = @"手机号免注册快速登录";
    signLab.font = [UIFont systemFontOfSize:13];
    signLab.textAlignment = NSTextAlignmentCenter;
    signLab.textColor = [UIColor yt_colorWithHexString:@"#949494"];
    [self.view addSubview:signLab];
    //手机号
    UITextField *phoneNum = [[UITextField alloc] init];
    phoneNum.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    phoneNum.placeholder = @"手机号";
    phoneNum.font = [UIFont systemFontOfSize:13];
    
    phoneNum.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    phoneNum.layer.borderWidth = 1.0;
    phoneNum.layer.cornerRadius = 3.0;
    phoneNum.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    phoneNum.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:phoneNum];
    self.phoneNum = phoneNum;
    phoneNum.tintColor = [UIColor blueColor];
    phoneNum.keyboardType = UIKeyboardTypePhonePad;
    //    [phoneNum becomeFirstResponder];
    //验证码
    UITextField *safeCode = [[UITextField alloc] init];
    safeCode.textColor = [UIColor yt_colorWithHexString:@"#685d5d"];
    safeCode.placeholder = @"验证码";
    safeCode.font = [UIFont systemFontOfSize:13];
    
    safeCode.layer.borderColor = [UIColor yt_colorWithHexString:@"#efefef"].CGColor;
    safeCode.layer.borderWidth = 1.0;
    safeCode.layer.cornerRadius = 3.0;
    safeCode.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 0)];
    safeCode.leftViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:safeCode];
    self.safeCode = safeCode;
    //发送验证码的按钮
    YTTimeButton *timeBtn = [YTTimeButton buttonWithType:UIButtonTypeCustom];
    [timeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [timeBtn setTitleColor:[UIColor yt_colorWithHexString:@"#685d5d"] forState:UIControlStateNormal];
    timeBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    timeBtn.layer.borderColor = [UIColor yt_colorWithHexString:@"#ff3366"].CGColor;
    timeBtn.layer.borderWidth = 1.0;
    timeBtn.layer.cornerRadius = 3.0;
    [self.view addSubview:timeBtn];
    [timeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    self.sendBtn = timeBtn;
    
    //登录按钮
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginBtn setTitle:@"登录" forState: UIControlStateNormal];
    [loginBtn setBackgroundColor:[UIColor yt_colorWithHexString:@"#ff3366"]];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:loginBtn];
    loginBtn.alpha = 0.45;
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    //打折宝登录
    UIButton *dzbBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [dzbBtn setTitle:@"使用打折宝账号登录" forState:UIControlStateNormal];
    [dzbBtn setTitleColor:[UIColor yt_colorWithHexString:@"#ff3366"] forState:UIControlStateNormal];
    dzbBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.view addSubview:dzbBtn];
    [dzbBtn addTarget:self action:@selector(dzbBtnClick) forControlEvents:UIControlEventTouchUpInside];
    //注册号登录
    UIButton *registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [registBtn setTitle:@"使用注册码登录" forState:UIControlStateNormal];
    registBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [registBtn setTitleColor:[UIColor yt_colorWithHexString:@"#ff3366"] forState:UIControlStateNormal];
    registBtn.alpha = 0.45;
    [self.view addSubview:registBtn];
    [registBtn addTarget:self action:@selector(registBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    // 返回按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateSelected];
    [backBtn sizeToFit];
    [backBtn addTarget:self action:@selector(backVC) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    
    //页面布局
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.view).offset(15);
    }];
    
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(60);
        make.height.mas_equalTo(1);
        make.width.mas_equalTo(KScreenW);
    }];
    
    [qqImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom).offset(kHdistance);
        make.left.equalTo(self.view).offset((KScreenW - 3*kHdistance - 4*qqImg.bounds.size.width)/2);
    }];
    
    [wxImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(qqImg);
        make.left.equalTo(qqImg.mas_right).offset(kHdistance);
    }];
    
    
    [tbImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(qqImg);
        make.left.equalTo(wxImg.mas_right).offset(kHdistance);
    }];
    
    [wbImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(qqImg);
        make.left.equalTo(tbImg.mas_right).offset(kHdistance);
    }];
    
    [qqLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qqImg.mas_bottom).offset(kVdistance);
        make.centerX.equalTo(qqImg);
    }];
    
    [wxLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wxImg.mas_bottom).offset(kVdistance);
        make.centerX.equalTo(wxImg);
    }];
    
    [tbLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tbImg.mas_bottom).offset(kVdistance);
        make.centerX.equalTo(tbImg);
    }];
    
    [wbLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wbImg.mas_bottom).offset(kVdistance);
        make.centerX.equalTo(wbImg);
    }];
    
    [signLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wxLab.mas_bottom).offset(kHdistance);
        make.centerX.equalTo(self.view);
    }];
    
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(1.5*kHdistance);
        make.centerY.equalTo(signLab);
        make.height.mas_equalTo(1);
        make.right.equalTo(signLab.mas_left).offset(-kVdistance);
    }];
    
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-1.5*kHdistance);
        make.centerY.equalTo(signLab);
        make.left.equalTo(signLab.mas_right).offset(kVdistance);
        make.height.mas_equalTo(1);
    }];
    
    [timeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-kHdistance);
        make.centerY.equalTo(phoneNum);
        make.left.equalTo(phoneNum.mas_right).offset(kVdistance);
        make.width.mas_equalTo(80);
    }];
    
    [phoneNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(kHdistance);
        make.top.equalTo(signLab.mas_bottom).offset(kHdistance);
        make.height.mas_equalTo(30);
    }];
    
    [safeCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(phoneNum.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
        make.height.mas_equalTo(30);
    }];
    
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(safeCode.mas_bottom).offset(4*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
    }];
    
    [dzbBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(2*kVdistance);
        make.left.equalTo(self.view).offset(kHdistance);
        make.right.equalTo(registBtn.mas_left);
    }];
    
    [registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loginBtn.mas_bottom).offset(2*kVdistance);
        make.right.equalTo(self.view).offset(-kHdistance);
        make.width.mas_equalTo(dzbBtn);
    }];
}

- (BOOL)prefersStatusBarHidden {
    
    return YES;
}


@end
